package com.slj.spring_security_demo.service;

import com.slj.spring_security_demo.config.DBUserDetailsManager;
import com.slj.spring_security_demo.entity.UserEntity;
import com.slj.spring_security_demo.repository.UserRepository;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserService {
    @Resource
    private DBUserDetailsManager dbUserDetailsManager;

    @Resource
    private UserRepository userRepository;

    @Resource
    private PasswordEncoder passwordEncoder;

    public void save(UserEntity userEntity) {
        UserDetails userDetails = User.withUsername(userEntity.getName())
                .password(passwordEncoder.encode(userEntity.getPassword()))
                .build();
        dbUserDetailsManager.createUser(userDetails);
    }

    public List<UserEntity> findAll() {
        return userRepository.findAll();
    }
}
